package ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.studyAndroid.R;

import java.util.List;


public class GridAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<String> domains;
    private Holder holder;

    public GridAdapter(Context context, List<String> domains) {
        this.domains = domains;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return domains.size();
    }

    @Override
    public Object getItem(int position) {
        return domains.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (null == convertView) {
            convertView = layoutInflater.inflate(R.layout.textview, parent, false);
            holder = new Holder();
            holder.textView = (TextView) convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.textView.setText(domains.get(position));
        if (position < 7) {
            holder.textView.setBackgroundResource(R.drawable.title_item_selector);
        } else if (position / 7%2 == 0) {
            holder.textView.setBackgroundResource(R.drawable.blue_item_selector);
        }   else if (position / 7%2 == 1) {
            holder.textView.setBackgroundResource(R.drawable.gray_item_selector);
        }
        return convertView;
    }

    private class Holder {
        TextView textView;
    }
}
